package com.we.wecc.aw.mid.impl;

import com.we.wecc.aw.db.mapper.AwardMapper;
import com.we.wecc.aw.db.mapper.AwardWinMapper;
import com.we.wecc.aw.db.model.Award;
import com.we.wecc.aw.db.model.AwardWin;
import com.we.wecc.aw.db.model.AwardWinExample;
import com.we.wecc.aw.mid.model.in.AwardWinIn;
import com.we.wecc.aw.mid.model.out.AwardWinOut;
import com.we.wecc.aw.mid.service.AwardWinService;
import com.we.wecc.us.db.mapper.UserAddressMapper;
import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.db.model.User;
import com.we.wecc.us.db.model.UserAddress;
import com.x.share.db.model.Pagination;
import com.x.share.mid.exception.HelperException;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AwardWinServiceImpl implements AwardWinService {

    @Resource
    UserMapper userMapper;

    @Resource
    AwardWinMapper awardWinMapper;

    @Resource
    AwardMapper awardMapper;

    @Resource
    UserAddressMapper userAddressMapper;

    @Override
    public AwardWin saveAwardWin(AwardWinIn in) {
        AwardWin as = in.toAwardWin();
        Award award = awardMapper.selectByPrimaryKey(as.getAwardId());
        if(award==null){
            throw new HelperException("该活动不存在！");
        }
        as.setCreateTime(new Date());
        if (as.getId() != null && as.getId() > 0) {
            AwardWin awardWin = awardWinMapper.selectByPrimaryKey(as.getId());
            if (awardWin == null) {
                throw new HelperException("该获奖人信息不存在！");
            }
            awardWinMapper.updateByPrimaryKeySelective(as);
        } else {
            awardWinMapper.insertSelective(as);
        }
        return as;
    }

    @Override
    public AwardWinOut getAwardWinByAwardId(int awardId) {
        if(awardId <= 0){
            throw new HelperException("该活动不存在！");
        }
        AwardWinOut awardWinOut = new AwardWinOut();
        AwardWin awardWin = awardWinMapper.selectByPrimaryAwardId(awardId);
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(awardWin.getAddressId());
        User user = userMapper.selectByPrimaryKey(awardWin.getUserId());
        BeanUtils.copyProperties(user, awardWinOut);
        BeanUtils.copyProperties(awardWin, awardWinOut);
        awardWinOut.setAddress(userAddress.getAddress());
        if(awardWin.getCashPrizeState()==1){
            awardWinOut.setAshPrize("已兑奖");
        }else{
            awardWinOut.setAshPrize("未兑奖");
        }
        return awardWinOut;
    }

    @Override
    public Pagination<AwardWinOut> getAwardWinList(Integer pageNo, Integer pageSize) {
        AwardWinExample ex = new AwardWinExample();
        ex.setOrderByClause("createTime desc");
        RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
        List<AwardWinOut> listAwardWin = new ArrayList<>();
        List<AwardWin> list = awardWinMapper.selectByExampleWithRowbounds(ex,rb);
        list.forEach(s -> {
            AwardWinOut awardWinOut = new AwardWinOut();
            User user = userMapper.selectByPrimaryKey(s.getUserId());
            BeanUtils.copyProperties(user, awardWinOut);
            BeanUtils.copyProperties(s, awardWinOut);
            UserAddress userAddress = userAddressMapper.selectByPrimaryKey(s.getAddressId());
            awardWinOut.setAddress(userAddress.getAddress());
            if(s.getCashPrizeState()==1){
                awardWinOut.setAshPrize("已兑奖");
            }else{
                awardWinOut.setAshPrize("未兑奖");
            }
            listAwardWin.add(awardWinOut);
        });
        int count = (int)awardWinMapper.countByExample(ex);
        Pagination<AwardWinOut> page = new Pagination<>(pageNo, pageSize);
        page.setItems(listAwardWin);
        page.setRecords(count);
        return page;
    }
}
